package web.Users;

import domain.Users;
import org.apache.commons.beanutils.BeanUtils;
import service.User_Service;
import service.impl.User_ServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/upUserServlet")
public class UpUserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*设置页面编码*/
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        //把上传的数据存入map集合
        Map<String,String[]> map = request.getParameterMap();
        Users users = new Users();
        try {
            BeanUtils.populate(users,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        System.out.println(users);

        //调用servic更新用户数据
        User_Service user_service = new User_ServiceImpl();
        if (user_service.upUser(users)){
            request.setAttribute("succes","修改成功");
        }else {
            request.setAttribute("Erorr","修改失败");
        }
        request.getRequestDispatcher("allUserServlet").forward(request,response);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
